/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.socialNet.dao.implementations;

import com.bionic.socialNet.dao.interfaces.IMessageDAO;
import com.bionic.socialNet.entities.MessageData;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ann
 */
public class OraMessage extends QueryExecutor implements IMessageDAO {

    private final String SELECT_USERS_MESSAGES = "select message_id, type, text_value, user_from, user_to, status from message m where " +
" m.user_from = %d and m.type = %d";
    private final String INSERT_MESSAGE = "insert into message (message_id, type, text_value, user_from, user_to, status)"
            + " values (SEQ_MESSAGE.Nextval, %d, '%s', %d, %d, %d)";
    // type,v_text_value,v_user_from,v_user_to,v_status
    private final String DELETE_MESSAGE = "delete message where message_id = %d;";
    // updates status only
    private final String UPDATE_MESSAGE = "update message  set  status = v_status where message_id = %d;";


    @Override
    public boolean insertMessage(MessageData message) {
        return query.executeInsertStatement(String.format(INSERT_MESSAGE, message.getType(), message.getTextValue(),
                message.getUser_from(), message.getUser_to(), message.getStatus()));
    }

    @Override
    public boolean deleteMessage(int messageID) {
        return query.executeDeleteStatement(String.format(DELETE_MESSAGE, messageID));
    }

    @Override
    public boolean updateMessage(int messageID) {
        return query.executeUpdateStatement(String.format(UPDATE_MESSAGE, messageID));
    }

    @Override
    public ArrayList<MessageData> selectMessagesCollection(int userID, int messageType) {

        ResultSet RS = query.getRSForQuery(String.format(SELECT_USERS_MESSAGES, userID,messageType));
        ArrayList<MessageData> messages = new ArrayList<MessageData>();

        try {
            while (RS.next()) {
                MessageData message = new MessageData();
                message.setMessage_id(RS.getInt("message_id"));
                message.setStatus(RS.getInt("status"));
                message.setTextValue(RS.getString("text_value"));
                message.setType(RS.getInt("type"));
                message.setUser_from(RS.getInt("user_from"));
                message.setUser_to(RS.getInt("user_to"));

                messages.add(message);
            }
        } catch (SQLException ex) {
            Logger.getLogger(OraUser.class.getName()).log(Level.SEVERE, null,
                    ex);
            return null;
        }
        return messages;
    }
}
